﻿using System;
using System.Windows.Forms;
using Ninject;
using System.Xml;

namespace gixoom.ClientShell
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            string personalDirectoryPath = Environment.GetFolderPath(
                Environment.SpecialFolder.Personal);

            if (string.IsNullOrEmpty(personalDirectoryPath))
            {
                // TODO: localize hardcoded strings
                MessageBox.Show("Cannot locate personal user directory.", "Message",
                    MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            else
            {
                var hostAddresses = GetHostAddresses();
                var module = new ClientShellModule(personalDirectoryPath, hostAddresses);
                var kernel = new StandardKernel(module);

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                var form = kernel.Get<ClientShellForm>();
                Application.Run(form);
            }
        }

        private static string[] GetHostAddresses()
        {
            // TODO: this is a temporary code. Actually hosts provider should be created and used as a service instead.
            var document = new XmlDocument();
            document.Load("Hosts.xml");

            XmlNodeList hosts = document.GetElementsByTagName("host");
            string[] addresses = new string[hosts.Count];
            int i = 0;

            foreach (XmlNode host in hosts)
            {
                string address = host.Attributes["address"].Value;
                addresses[i++] = address;
            }

            return addresses;
        }
    }
}
